"""
银行接口层
"""
import datetime
import logging

from db.db_handler import save
from lib.common import pretty_print

logger = logging.getLogger(__name__)


def check_balance_interface(user_dict: dict):
    """查看余额接口"""
    print('当前余额：')
    title_list = ['用户名', '余额']  # 标题列表
    data_list = [[user_dict['username'], user_dict['balance']]]  # 数据列表
    pretty_print(title_list, data_list)  # 美化打印


def recharge_interface(user_dict: dict, amount: str):
    """充值接口"""
    # 增加余额
    user_dict['balance'] += float(amount)
    # 记录流水
    user_dict['flow'].append(f"{datetime.datetime.now()} 用户{user_dict['username']}成功充值{amount}元")
    # 保存数据
    save(user_dict)
    # 记录日志
    print(f"{datetime.datetime.now()} 用户{user_dict['username']}成功充值{amount}元")
    logger.info(f"{datetime.datetime.now()} 用户{user_dict['username']}成功充值{amount}元")
    # 打印余额
    check_balance_interface(user_dict)


def withdraw_interface(user_dict: dict, total_amount: float, amount: float):
    """提现"""
    # 更新余额
    user_dict['balance'] -= total_amount
    # 记录流水
    user_dict['flow'].append(
        f"{datetime.datetime.now()} 用户{user_dict['username']}成功提现{amount}元 手续费{total_amount - amount}")
    # 保存数据
    save(user_dict)
    # 记录日志
    print(f"{datetime.datetime.now()} 用户{user_dict['username']}成功提现{amount}元 手续费{total_amount - amount}")
    logger.info(f"{datetime.datetime.now()} 用户{user_dict['username']}成功提现{amount}元 手续费{total_amount - amount}")
    # 打印余额
    check_balance_interface(user_dict)


def transfer_interface(user_dict, target_user_dict, amount):
    """转账接口"""
    # 转账
    user_dict['balance'] -= amount
    target_user_dict['balance'] += amount
    # 记录流水
    user_dict['flow'].append(
        f"{datetime.datetime.now()} 用户{user_dict['username']}转账给用户{target_user_dict['username']}{amount}元")
    target_user_dict['flow'].append(
        f"{datetime.datetime.now()} 用户{target_user_dict['username']}收到用户{user_dict['username']}的转账{amount}元")
    # 保存数据
    save(user_dict)
    save(target_user_dict)
    # 记录日志
    print(
        f"{datetime.datetime.now()} 用户{user_dict['username']}成功转账给用户{target_user_dict['username']}{amount}元")
    logger.info(
        f"{datetime.datetime.now()} 用户{user_dict['username']}成功转账给用户{target_user_dict['username']}{amount}元")
    # 打印余额
    check_balance_interface(user_dict)


def check_flow_interface(user_dict: dict):
    """查看流水接口"""
    title_list = ['详细流水']
    data_list = [[flow] for flow in user_dict['flow']]
    pretty_print(title_list, data_list)  # 美化打印
